package com.ruoyi.psi.base.cost.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.psi.base.cost.domain.ProductionCostCond;
import com.ruoyi.psi.base.cost.domain.ProductionCostDomain;
import com.ruoyi.psi.base.cost.domain.ProductionCostDto;
import com.ruoyi.psi.base.cost.service.IProductionCostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 视图层
 * 产品成本
 */
@RestController
@RequestMapping("/psi/production/cost")
@Api(value = "产品成本信息模块", tags = "产品成本接口")
public class ProductionCostController extends BaseController {
    @Autowired
    private IProductionCostService productionCostService;

    /**
     * 获取产品成本列表
     * @return
     */
    @ApiOperationSupport(includeParameters = {"ProductionCostCond.produceId"})
    @ApiOperation(value = "获取产品成本列表",notes = "查询所有产品成本信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ProductionCostCond cond) {
        startPage();
        List<ProductionCostDto> productionCostDtos = productionCostService.selectProductionCostList(cond);
        return getDataTable(productionCostDtos);
    }

    /**
     * 新增产品成本
     *
     * @param productionCost
     * @return
     */
    @ApiOperation(value = "新增产品成本记录", notes = "新增产品成本信息")
    @ApiImplicitParam(name = "productionCost", value = "新增产品成本", dataType = "ProductionCostDomain")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody ProductionCostDomain productionCost) {
        try {
            productionCostService.insertProductionCost(productionCost);
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 修改产品成本
     *
     * @param productionCost
     * @return
     */
    @ApiOperation(value = "修改产品成本", notes = "修改产品成本信息")
    @ApiImplicitParam(name = "productionCost", value = "修改产品成本", dataType = "ProductionCostDomain")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody ProductionCostDomain productionCost) {
//        System.out.println("请求接口启动");
        try {
//            System.out.println("请求接口启动222222");
            productionCostService.updateProductionCost(productionCost);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除产品成本记录
     * 仅修改删除标志位
     *
     * @param productionCost
     * @return
     */
    @ApiOperation(value = "删除产品成本", notes = "删除产品成本,仅删除标志位")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody ProductionCostDomain productionCost) {
        return toAjax(productionCostService.updateProductionCostDeleteFlag(productionCost));
    }
}
